package com.qifan.alb2b.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.qifan.alb2b.action.back.param.UserQueryParam;
import com.qifan.alb2b.dao.CompanyInfoDao;
import com.qifan.alb2b.dao.UserInfoDao;
import com.qifan.alb2b.domain.CompanyInfoDomain;
import com.qifan.alb2b.domain.OrderInfoDomain;
import com.qifan.alb2b.domain.UserInfoDomain;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.vo.UserVo;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

@Service
public class UserInfoService {
	private UserInfoDao userInfoDao;
	private CompanyInfoDao companyInfoDao;

	public void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}
	
	public void setCompanyInfoDao(CompanyInfoDao companyInfoDao) {
		this.companyInfoDao = companyInfoDao;
	}

	public Page query(DataGridModel dgm,UserQueryParam uqp){
		return userInfoDao.query(dgm,uqp);
	}
	
	public void add(UserInfoDomain userInfoDomain){
		userInfoDao.add(userInfoDomain);
	}
	
	public void update(UserInfoDomain userInfoDomain){
		userInfoDao.update(userInfoDomain);
	}
	
	public void delete(String[] ids){
		userInfoDao.delete(ids);
	}
	public void updateStatus(String[] ids,String status){
		userInfoDao.updateStatus(ids,status);
	}
	
	/**
	 * 根据用户名和密码返回UserVo
	 * @param userName
	 * @param password
	 * @return
	 */
	public UserVo getUser(String userName,String password){
		 UserInfoDomain userInfo =userInfoDao.getUser(userName, password);
		 if(userInfo!=null){
			 if(userInfo.getStatus()!=Constants.NOMARL)
				 throw new BusinessException("您的帐号未审核通过或者已停用，请联系管理员");
			 CompanyInfoDomain companyInfo=companyInfoDao.getCompanyById(userInfo.getCompId());
			 UserVo userVo=new UserVo();
			 userVo.setCompanyInfo(companyInfo);
			 userVo.setUserInfo(userInfo);
			 return userVo;
		 }else{
			 throw new BusinessException("用户名或密码错误！");	
		 }
	}
	public boolean getUserByUserName(String userName){
		 UserInfoDomain userInfoDomain =userInfoDao.getUserByUserName(userName);
		 if(userInfoDomain==null){
			 return true;
		 }else{
			 throw new BusinessException("个人用户名已经存在，请更换用户名！");	
		 }
	}
	public UserInfoDomain getUserById(String id){
		 return userInfoDao.getUserById(id);
		 
	}
	public List<OrderInfoDomain> getOrderByUserId(String userId){
		
		return userInfoDao.getOrderByUserId(userId);
	}
}
